<template>
  <el-dialog :title="info.isadd ? '添加角色' : '编辑角色'" :visible.sync="info.isshow" center :before-close="cancel">
    <el-form :model="userinfo" :rules="rules" ref="form">
      <el-form-item label="所属角色" label-width="120px" prop="roleid">
        <el-select v-model="userinfo.roleid" placeholder="请选择">
          <el-option
            v-for="item in get_roleList"
            :key="item.id"
            :label="item.rolename"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用户名" label-width="120px" prop="username">
        <el-input v-model="userinfo.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" label-width="120px">
        <el-input v-model="userinfo.password"></el-input>
        <p v-if="info.isadd==false">密码不写则为不修改</p>
      </el-form-item>

      <el-form-item label="状态" label-width="120px">
        <el-switch
          v-model="userinfo.status"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="2"
        ></el-switch>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="sure">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
//------------------------ 重置，编辑（弹框，数据回显）----------------------------
import { mapGetters, mapActions } from "vuex";
import {useradd,userinfo,useredit} from "@/request/api.js"
export default {
  props: ["info"],
  data() {
    return {
      //管理员管理数据
      userinfo: {
        roleid: "", //角色编号
        username: "",
        password: "",
        status: 1 //状态 1：正常  2：禁用  number
      },
      //表单验证规则
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2 到 8 个字符", trigger: "blur" }
        ],
        roleid: [
          { required: true, message: "请选择角色编号", trigger: "change" }
        ]
      }
    };
  },
  methods: {
    ...mapActions({
      action_roleList: "role/action_roleList", //获取角色数据的方法
      action_userList: "user/action_userList", //获取管理员数据的方法
      action_count:"user/action_count" //获取管理员总条数

    }),
    //1.取消  -- 通知父组件，关闭弹框
    cancel() {
      this.$emit("cancel");
      this.reset();
    },

    //2.重置
    reset(){
      //数据重置
      this. userinfo = {
        roleid: "", //角色编号
        username: "",
        password: "",
        status: 1 //状态 1：正常  2：禁用  number
      }
      //验证重置
      this.$refs.form.resetFields();
    },

    //2.点击确定
    sure() {
      //通过验证才能往下操作
      this.$refs.form.validate(async val => {
        if (!val) {
          return;
        }

        //添加
        if (this.info.isadd) {

          try{
            //发起请求
            let res = await useradd(this.userinfo);
            if(res.code == 200){
              //提示，关闭弹框，页面发生变化
              this.$message.success(res.msg);
              this.cancel();
              this.action_userList();
              //调用重新获取页码
              this.action_count();

            }else{
              this.$message.info(res.msg);
            }
          }catch(err){
            this.$message.error(err)
          }

        } else {
          try{
            //发起请求
            let res = await useredit(this.userinfo);
            if(res.code == 200){
              //提示，关闭弹框，页面发生变化
              this.$message.success(res.msg);
              this.cancel();
              this.action_userList();

            }else{
              this.$message.info(res.msg);
            }
          }catch(err){
            this.$message.error(err)
          }
        }
        
      });
    },
   
    //3.数据回显
    async lookup(uid){
      try{
        let res = await userinfo({uid});
        if(res.code == 200){
          this.userinfo = res.list;
          //修改需要多一个字段，uid，现在在能拿到uid的地方先添加上
          this.userinfo.uid = uid;
          //密码字段不应该给用户看
          this.userinfo.password = "";
        }else{
          this.$message.info(res.msg);
        }
      }catch(err){
        this.$message.error(err);
      }
    }
  },
  computed: {
    ...mapGetters({
      get_roleList: "role/get_roleList"
    })
  },
  //页面加载后请求角色数据
  mounted() {
    if (this.action_roleList.length == 0) {
      this.action_roleList();
    }
  }
};
</script>

<style>
</style>